package com.web.portal.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.web.portal.pojo.Activity;
import com.web.portal.pojo.PortalUser;
import com.web.portal.pojo.query.ActivityQuery;
import com.web.portal.pojo.response.ActivityAndUser;
import com.web.portal.response.ResponseResult;
import com.web.portal.services.ActivityService;
import com.web.portal.services.ICategoryService;
import com.web.portal.services.UserService;
import com.web.portal.utils.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * 活动
 */
@Api(tags = "活动管理")
@Slf4j
@RestController
@RequestMapping("/admin/activity")
public class ActivityAdminApi {
    @Autowired
    private ActivityService activityService;

    @Autowired
    private ICategoryService iCategoryService;

    @Autowired
    private Gson gson;

    @Autowired// 阔以表明引用哪个类
    private UserService userService;

    @Autowired
    private IdWorker idWorker;

    /**
     * 添加活动
     * 需要管理员权限
     *
     * @param activity
     * @return
     */
    @ApiOperation(value = "添加活动")
    @PostMapping
    public ResponseResult addActivity(@RequestBody Activity activity) {
        activity.setActivityCode(idWorker.nextId()+"");
        boolean flag = activityService.save(activity);
        if (flag) {
            return ResponseResult.SUCCESS("添加成功");
        } else {
            return ResponseResult.FAILED("添加失败");
        }
    }

    /**
     * 获取活动
     *
     * @param activityId
     * @return
     */
    @ApiOperation(value = "用id获取活动")
    @GetMapping("/{activityId}")
    public ResponseResult getActivity(@PathVariable("activityId") String activityId) {
        Activity activity = activityService.getById(activityId);
        if (activity == null) {
            return ResponseResult.FAILED("查找失败");
        }
        activity.setType(getType(activity.getType()));
        return ResponseResult.SUCCESS().setData(activity);
    }

    /**
     * 用id获取活动
     * @param activityId
     * @return
     */
    @ApiOperation(value = "用id获取 活动和参与用户")
    @GetMapping("/activity_and_user/{activityId}")
    public ResponseResult getActivityAndUser(@PathVariable("activityId") String activityId) {
        Activity activity = activityService.getById(activityId);
        if (activity == null) {
            return ResponseResult.FAILED("查找失败");
        }
        activity.setType(getType(activity.getType()));
        String projectJson = gson.toJson(activity);
        ActivityAndUser activityAndUser = gson.fromJson(projectJson, ActivityAndUser.class);

        String participants = activity.getParticipants();
        String[] split = participants.split(",");
        ArrayList<PortalUser> portalUsers = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            PortalUser portalUser = userService.selectByUserCodeInfo(split[i]);
            if (portalUser != null) {
                portalUsers.add(portalUser);
            }
        }
        activityAndUser.setPortalUser(portalUsers);
        return ResponseResult.SUCCESS().setData(activityAndUser);
    }

    /**
     * 用ActivityCode获取活动
     * @param activityCode
     * @return
     */
    @ApiOperation(value = "用activityCode获取活动和参与用户")
    @GetMapping("/activity_and_user_bycode/{activityCode}")
    public ResponseResult getActivityAndUserByActivityCode(@PathVariable("activityCode") String activityCode) {
        Activity activity = activityService.selectActivityByActivityCode(activityCode);
        if (activity == null) {
            return ResponseResult.FAILED("查找失败");
        }
        activity.setType(getType(activity.getType()));
        String projectJson = gson.toJson(activity);
        ActivityAndUser activityAndUser = gson.fromJson(projectJson, ActivityAndUser.class);

        String participants = activity.getParticipants();
        String[] split = participants.split(",");
        ArrayList<PortalUser> portalUsers = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            PortalUser portalUser = userService.selectByUserCodeInfo(split[i]);
            if (portalUser != null) {
                portalUsers.add(portalUser);
            }
        }
        activityAndUser.setPortalUser(portalUsers);
        return ResponseResult.SUCCESS().setData(activityAndUser);
    }

    /**
     * 获取活动列表
     * <p>
     * 权限：管理员权限
     *
     * @return
     */
    @ApiOperation(value = "获取活动列表")
    @GetMapping("/list")
    public ResponseResult listActivitys() {
        return ResponseResult.SUCCESS("获取分类列表成功.")
                .setData(activityService.list());
    }

    //3 条件查询带分页
    @ApiOperation(value = "条件(类型)查询带分页")
    @PostMapping("/findPageActivity/{current}/{limit}")
    public ResponseResult findPageActivitys(@PathVariable long current,
                                          @PathVariable long limit,
                                          @RequestBody(required = false) ActivityQuery activityQuery) {
        //创建page对象，传递当前页，每页记录数
        Page<Activity> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();

        String hosname = activityQuery.getType();//获取类型
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.eq("type", activityQuery.getType());
        }
        wrapper.orderByDesc("id");
        //调用方法实现分页查询
        Page<Activity> pageHospitalSet = activityService.page(page, wrapper);
        //返回结果
        return ResponseResult.SUCCESS("获取活动列表成功.")
                .setData(pageHospitalSet);
    }

    /**
     * 更新活动
     *
     * @param activity
     * @return
     */
    @ApiOperation(value = "更新活动")
    @PostMapping("/updateActivity")
    public ResponseResult updateActivity(@RequestBody Activity activity) {
        boolean flag = activityService.updateById(activity);
        if (flag) {
            return ResponseResult.SUCCESS("更改成功");
        } else {
            return ResponseResult.FAILED("更改失败");
        }
    }

    /**
     * 用 id 获取活动
     *
     * @param activityId
     * @return
     */
    @ApiOperation(value = "用 id 删除活动")
    @DeleteMapping("/{activityId}")
    public ResponseResult deleteActivity(@PathVariable("activityId") String activityId) {
        boolean flag = activityService.removeById(activityId);
        if (flag) {
            return ResponseResult.SUCCESS("删除成功");
        } else {
            return ResponseResult.FAILED("删除失败");
        }
    }

    public String getType(String type){
        switch (type){
            case "1":
                return "比赛";
            case "2":
                return "党员";
            case "3":
                return "团建";
            case "4":
                return "聚餐";
            default:
                return type;
        }
    }
}
